package com.chnbst.bstyun.controller.user;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.OtherFile;
import com.chnbst.bstyun.service.OtherFileService;

/**
 * @author zqh
 * @date 2014年9月22日
 * 
 */

@Controller
@RequestMapping("/otherfile")
public class OtherFileDownController {
	
	@Autowired
	private OtherFileService otherFileService;

	/**
	 * 下载文件
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/download",method=RequestMethod.GET)
	public void downFile(HttpServletResponse response, HttpServletRequest request){
		int downloadId = Integer.parseInt(request.getParameter("id"));
		OtherFile otherFile = otherFileService.getOtherFileById(downloadId);
		String path = null;
		if(otherFile != null){
			path = otherFile.getDownloadUrl();
		}
		path=request.getRealPath(path);
		try {
			download(response, path);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 下载文件方法
	 * @param response
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static HttpServletResponse download(HttpServletResponse response,String path) throws IOException {
		 try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.setHeader("Content-disposition", "attachment; filename="  
			        + new String(filename.getBytes("utf-8"), "ISO8859-1"));
            response.setHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
	}
	
	/**
	 * 获取其它下载列表内容
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/filelist",method=RequestMethod.GET)
	public ModelAndView getDownloadFile(HttpServletRequest request){
		List<OtherFile> list = otherFileService.getOtherFile();
		ModelAndView model = new ModelAndView();
		model.addObject(list);
		model.setViewName("");
		return model;
	}
	
}
